#ifndef _MY_TOOLS_H_
#define _MY_TOOLS_H_

#include <stdio.h>

static inline
void bit_print_char (char a)
{
    int i;
    int n = sizeof(char) * 8;
    int mask = 1 << (n-1);

    for (i = 1; i <= n; i++) {
        putchar(((a & mask) == 0) ? '0' : '1');
        a <<= 1;
        if (i % 8 == 0) {
            putchar(' ');
        }
    }

    printf("\n");
}

/*
 * print the bit form of an integer
 * from the most significant bit to the little significant bit
 */
static inline
void bit_print_int (int a)
{
    int i;
    int n = sizeof(int) * 8;
    int mask = 1 << (n-1);

    for (i = 1; i <= n; i++) {
        putchar(((a & mask) == 0) ? '0' : '1');
        a <<= 1;
        if (i % 8 == 0) {
            putchar(' ');
        }
    }

    printf("\n");
}
    
static inline
void hex_print (char *des, void *start, int length)
{
    int i;
    unsigned char *p;

    printf(">>  %s [%d bytes]\n", des, length);

    p = (unsigned char*)start;
    for (i=0; i<length; i++) {
        if (i % 8 == 0) {
            if (i % 16 == 0) {
                if (i != 0) {
                    printf("\n");
                }
                printf("0x%08x:  ", (unsigned int)(p+i));
            } else {
                printf("  ");
            }
        }
        printf("%.2X ", *(p+i));
    }
    printf("\n");
}

#endif
